package com.pz.cloud.framework.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang3.StringUtils;

import java.util.*;


public class JsonUtil {
	
	public static void printLog(Object obj){
		String info=getJsonString(obj);
		System.out.println("info====>"+info);
	}
	
	@SuppressWarnings({"rawtypes"})
	public static Object getObject4JsonString(String jsonString, Class pojoCalss,String datePattern[]){
	     JSONObject jsonObject = JSONObject.fromObject(jsonString);
	     Object pojo = JSONObject.toBean(jsonObject, pojoCalss);
	     return pojo;
	}
	/**
	 * 
	 * @param jsonString
	 * @param pojoCalss
	 * @param classMap  有集合时 集合的转换类[注意key 应与bean中的元素名一致]
	 * @return
	 */
	@SuppressWarnings({"rawtypes"})
	public static Object getObject4JsonString(String jsonString, Class pojoCalss,Map<String, Class> classMap) {
		
		//当有数据过大时 传转为科学计数 导致保存不了
//		JSONObject jsonObject = JSONObject.fromObject(jsonString);
//		Object pojo = com.alibaba.fastjson.JSONObject.toBean(jsonObject, pojoCalss,classMap);
//		return pojo;
		
		//啊里的包
//		com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) com.alibaba.fastjson.JSONObject.parse(jsonString);

//		return jsonObject;
		return null;
	}
	

	public static Map getMap4Json(String jsonString) {
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		Iterator keyIter = jsonObject.keys();

		Map valueMap = new HashMap();

		while (keyIter.hasNext()) {
			String key = (String) keyIter.next();
			Object value = jsonObject.get(key);
			valueMap.put(key, value);
		}

		return valueMap;
	}

	public static Object[] getObjectArray4Json(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		return jsonArray.toArray();
	}

	@SuppressWarnings({"unchecked","rawtypes"})
	public static List getList4Json(String jsonString, Class pojoClass,String[] datePattern) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);

		List list = new ArrayList();
		for (int i = 0; i < jsonArray.size(); ++i) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			Object pojoValue = JSONObject.toBean(jsonObject, pojoClass);
			if(pojoValue instanceof Map){
				HashMap<String, Object> valMap=(HashMap<String, Object>)pojoValue;
				if(valMap.containsKey("record")){
					String recordInfo=valMap.get("record").toString();
					if(recordInfo.contains("net.")){    //去掉页面附带的record值（保存会报错）
						valMap.remove("record");
					}
				}
			}
			list.add(pojoValue);
		}
		return list;
	}
	@SuppressWarnings({"rawtypes"})
	public static Object getObject4Json(String jsonString, Class pojoClass,String[] datePattern){
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		Object pojoValue = JSONObject.toBean(jsonObject, pojoClass);
		return pojoValue;
	}
	@SuppressWarnings({"unchecked","rawtypes"})
	public static List getList4Json(String jsonString, Class pojoClass) {
		
		
		 JsonConfig jsonConfig = new JsonConfig();  
	    //java.sql.Timestamp
	    jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessor()); 
	    JSONArray jsonArray = JSONArray.fromObject(jsonString,jsonConfig);
		List list = new ArrayList();
		for (int i = 0; i < jsonArray.size(); ++i) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			Object pojoValue = JSONObject.toBean(jsonObject, pojoClass);
			list.add(pojoValue);
		}
		
		
		return list;
	}
	public static List getList4Json(String jsonString) {
		List list = new ArrayList();
		if(StringUtils.isNotEmpty(jsonString)){
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessor());
			JSONArray jsonArray = JSONArray.fromObject(jsonString,jsonConfig);
			for (int i = 0; i < jsonArray.size(); ++i) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				Iterator keyIter = jsonObject.keys();

				Map valueMap = new HashMap();

				while (keyIter.hasNext()) {
					String key = (String) keyIter.next();
					Object value = jsonObject.get(key);
					valueMap.put(key, value);
				}
				list.add(valueMap);
			}
		}
		return list;
	}
	public static String[] getStringArray4Json(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		String[] stringArray = new String[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); ++i) {
			stringArray[i] = jsonArray.getString(i);
		}

		return stringArray;
	}

	public static Long[] getLongArray4Json(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Long[] longArray = new Long[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); ++i) {
			longArray[i] = Long.valueOf(jsonArray.getLong(i));
		}

		return longArray;
	}

	public static Integer[] getIntegerArray4Json(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Integer[] integerArray = new Integer[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); ++i) {
			integerArray[i] = Integer.valueOf(jsonArray.getInt(i));
		}

		return integerArray;
	}

	public static Double[] getDoubleArray4Json(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Double[] doubleArray = new Double[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); ++i) {
			doubleArray[i] = Double.valueOf(jsonArray.getDouble(i));
		}

		return doubleArray;
	}

	public static String getJsonString(Object obj) {
		JSONObject json = JSONObject.fromObject(obj);
		return json.toString();
	}
	@SuppressWarnings({"rawtypes"})
	public static String getJsonString4JavaList(List javaList,Class dateClass) {
	    JsonConfig jsonConfig = new JsonConfig();  
	    //java.sql.Timestamp
	    jsonConfig.registerJsonValueProcessor(dateClass, new JsonDateValueProcessor()); 
		JSONArray json = JSONArray.fromObject(javaList,jsonConfig);
		return json.toString();
	}
	@SuppressWarnings({"rawtypes"})
	public static String getJsonAndFormatDate(Object javaObj,
			Class dateClass) {
	    JsonConfig jsonConfig = new JsonConfig();  
	    //java.sql.Timestamp
	    jsonConfig.registerJsonValueProcessor(dateClass, new JsonDateValueProcessor()); 
	    
		JSONObject json = JSONObject.fromObject(javaObj,jsonConfig);
		
		return json.toString();
	}

	public static void main(String[] args) {
		 List<Map<String,Object>> list =new ArrayList<>();
		 Map<String, Object> returnMap=new LinkedHashMap<String, Object>();
		 Map<String, Object> d=new LinkedHashMap<String, Object>();
		 Map<String, Object> page=new LinkedHashMap<String, Object>();
		 Map<String,Object>accountInfo=new HashMap<>();
		 accountInfo.put("userId", "402884305f33d1ed015f33eadd350002");
		 accountInfo.put("loginName", "nihao");
		 accountInfo.put("userName", "系统管理员");
		 accountInfo.put("phone", "15824512987");
		 accountInfo.put("salt", "1d11a5d257da6cee7c27acd0d29eee4c");
		 accountInfo.put("password", "ea61e954f80351a0d110b62d25758092");
		 accountInfo.put("state", "0");
		 accountInfo.put("isAdmin", "0");
		 
		 d.put("pk", "9173AEDD-ABF3-43BD-AE08-71B8FF7A7962");
		 d.put("unitName", "佛山山丰股份有限公司");
		 d.put("unifiedCode", "98415454861897189F");
		 d.put("address", "广东省广州市越秀区大德路268号");
		 d.put("unitProvince", "140000");
		 d.put("unitCity", "140100");
		 d.put("unitArea", "440101");
		 d.put("unitType", "建设单位");
		 d.put("licenseNum", "");
		 d.put("unitRegType", "国有企业");
		 d.put("gpsX", "113.267012");
		 d.put("gpsY", "23.124168");
		 d.put("gpsZ", "");
		 d.put("postcode", "");
		 d.put("contacts", "");
		 d.put("contactsTel", "");
		 d.put("contactsCellPhone", "");
		 d.put("artificial", "");
		 d.put("artificialTel", "");
		 d.put("artificialID", "");
		 d.put("artificialDuty", "");
		 d.put("artificialTitle", "");
		 d.put("formDate", "2015-04-17");
		 d.put("registerCapital", "");	
		 d.put("capital", "");
		 d.put("unitMail", "");
		 d.put("unitWeb", "");
		 d.put("unitTel", "");
		 d.put("unitFax", "");
		 d.put("note", "");
		 d.put("updateDate", "2017-05-20");
		 d.put("customerId", "44a0800019");
		 d.put("accountInfo", accountInfo);
		 
		 
		 list.add(d);
		 
		 page.put("pageNo", "1");
		 page.put("pageSize", "1000");
		 page.put("totalCount", "425");
		 page.put("totalPage", "1");
		 
		 returnMap.put("code","001");
		 returnMap.put("msg","成功");
		 returnMap.put("page", page);
		 returnMap.put("list", list);
		 printLog(returnMap);
	}

	
	@SuppressWarnings({"rawtypes"})
	public static Object getObj4Json(String jsonString,Class pojoCalss,Map<String, Class> classMap){
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setRootClass(pojoCalss);
		jsonConfig.setClassMap(classMap);
		return JSONObject.toBean(jsonObject, jsonConfig);
	}
}
